home *** CD-ROM | disk | FTP | other *** search
/ Nautilus 1992 July / Nautilus-3-8 / Nautilus-3-8.bin / Tools & Utilities / Techy Stuff / Development Environments ƒ / Perl 4.0.2 ƒ / doio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-05  |  59.0 KB  |  2,842 lines

  1. /* $RCSfile: doio.c,v $$Revision: 4.0.1.4 $$Date: 91/11/05 16:51:43 $
  2.  *
  3.  *    Copyright (c) 1991, Larry Wall
  4.  *
  5.  *    You may distribute under the terms of either the GNU General Public
  6.  *    License or the Artistic License, as specified in the README file.
  7.  *
  8.  * $Log:    doio.c,v $
  9.  * Revision 4.0.1.4  91/11/05  16:51:43  lwall
  10.  * patch11: prepared for ctype implementations that don't define isascii()
  11.  * patch11: perl mistook some streams for sockets because they return mode 0 too
  12.  * patch11: reopening STDIN, STDOUT and STDERR failed on some machines
  13.  * patch11: certain perl errors should set EBADF so that $! looks better
  14.  * patch11: truncate on a closed filehandle could dump
  15.  * patch11: stats of _ forgot whether prior stat was actually lstat
  16.  * patch11: -T returned true on NFS directory
  17.  * 
  18.  * Revision 4.0.1.3  91/06/10  01:21:19  lwall
  19.  * patch10: read didn't work from character special files open for writing
  20.  * patch10: close-on-exec wrongly set on system file descriptors
  21.  * 
  22.  * Revision 4.0.1.2  91/06/07  10:53:39  lwall
  23.  * patch4: new copyright notice
  24.  * patch4: system fd's are now treated specially
  25.  * patch4: added $^F variable to specify maximum system fd, default 2
  26.  * patch4: character special files now opened with bidirectional stdio buffers
  27.  * patch4: taintchecks could improperly modify parent in vfork()
  28.  * patch4: many, many itty-bitty portability fixes
  29.  * 
  30.  * Revision 4.0.1.1  91/04/11  17:41:06  lwall
  31.  * patch1: hopefully straightened out some of the Xenix mess
  32.  * 
  33.  * Revision 4.0  91/03/20  01:07:06  lwall
  34.  * 4.0 baseline.
  35.  * 
  36.  */
  37.  
  38. #include "EXTERN.h"
  39. #include "perl.h"
  40.  
  41. #ifdef HAS_SOCKET
  42. #include <sys/socket.h>
  43. #include <netdb.h>
  44. #endif
  45.  
  46. #ifdef HAS_SELECT
  47. #ifdef I_SYS_SELECT
  48. #ifndef I_SYS_TIME
  49. #include <sys/select.h>
  50. #endif
  51. #endif
  52. #endif
  53.  
  54. #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
  55. #include <sys/ipc.h>
  56. #ifdef HAS_MSG
  57. #include <sys/msg.h>
  58. #endif
  59. #ifdef HAS_SEM
  60. #include <sys/sem.h>
  61. #endif
  62. #ifdef HAS_SHM
  63. #include <sys/shm.h>
  64. #endif
  65. #endif
  66.  
  67. #ifdef I_PWD
  68. #include <pwd.h>
  69. #endif
  70. #ifdef I_GRP
  71. #include <grp.h>
  72. #endif
  73. #ifdef I_UTIME
  74. #include <utime.h>
  75. #endif
  76. #ifdef I_FCNTL
  77. #include <fcntl.h>
  78. #endif
  79. #ifdef I_SYS_FILE
  80. #include <sys/file.h>
  81. #endif
  82.  
  83. int laststatval = -1;
  84. int laststype = O_STAT;
  85.  
  86. bool
  87. do_open(stab,name,len)
  88. STAB *stab;
  89. register char *name;
  90. int len;
  91. {
  92.     FILE *fp;
  93.     register STIO *stio = stab_io(stab);
  94.     char *myname = savestr(name);
  95.     int result;
  96.     int fd;
  97.     int writing = 0;
  98.     char mode[3];        /* stdio file mode ("r\0" or "r+\0") */
  99.     FILE *saveifp = Nullfp;
  100.     FILE *saveofp = Nullfp;
  101.     char savetype = ' ';
  102.  
  103.     name = myname;
  104.     forkprocess = 1;        /* assume true if no fork */
  105.     while (len && isSPACE(name[len-1]))
  106.     name[--len] = '\0';
  107.     if (!stio)
  108.     stio = stab_io(stab) = stio_new();
  109.     else if (stio->ifp) {
  110.     fd = fileno(stio->ifp);
  111.     if (stio->type == '-')
  112.         result = 0;
  113.     else if (fd <= maxsysfd) {
  114.         saveifp = stio->ifp;
  115.         saveofp = stio->ofp;
  116.         savetype = stio->type;
  117.         result = 0;
  118.     }
  119.     else if (stio->type == '|')
  120.         result = mypclose(stio->ifp);
  121.     else if (stio->ifp != stio->ofp) {
  122.         if (stio->ofp) {
  123.         result = fclose(stio->ofp);
  124.         fclose(stio->ifp);    /* clear stdio, fd already closed */
  125.         }
  126.         else
  127.         result = fclose(stio->ifp);
  128.     }
  129.     else
  130.         result = fclose(stio->ifp);
  131.     if (result == EOF && fd > maxsysfd)
  132.         fprintf(stderr,"Warning: unable to close filehandle %s properly.\n",
  133.           stab_name(stab));
  134.     stio->ofp = stio->ifp = Nullfp;
  135.     }
  136.     if (*name == '+' && len > 1 && name[len-1] != '|') {    /* scary */
  137.     mode[1] = *name++;
  138.     mode[2] = '\0';
  139.     --len;
  140.     writing = 1;
  141.     }
  142.     else  {
  143.     mode[1] = '\0';
  144.     }
  145.     stio->type = *name;
  146.     if (*name == '|') {
  147.     /*SUPPRESS 530*/
  148.     for (name++; isSPACE(*name); name++) ;
  149. #ifdef TAINT
  150.     taintenv();
  151.     taintproper("Insecure dependency in piped open");
  152. #endif
  153.     fp = mypopen(name,"w");
  154.     writing = 1;
  155.     }
  156.     else if (*name == '>') {
  157. #ifdef TAINT
  158.     taintproper("Insecure dependency in open");
  159. #endif
  160.     name++;
  161.     if (*name == '>') {
  162.         mode[0] = stio->type = 'a';
  163.         name++;
  164.     }
  165.     else
  166.         mode[0] = 'w';
  167.     writing = 1;
  168.     if (*name == '&') {
  169.       duplicity:
  170.         name++;
  171.         while (isSPACE(*name))
  172.         name++;
  173.         if (isDIGIT(*name))
  174.         fd = atoi(name);
  175.         else {
  176.         stab = stabent(name,FALSE);
  177.         if (!stab || !stab_io(stab)) {
  178. #ifdef EINVAL
  179.             errno = EINVAL;
  180. #endif
  181.             goto say_false;
  182.         }
  183.         if (stab_io(stab) && stab_io(stab)->ifp) {
  184.             fd = fileno(stab_io(stab)->ifp);
  185.             if (stab_io(stab)->type == 's')
  186.             stio->type = 's';
  187.         }
  188.         else
  189.             fd = -1;
  190.         }
  191.         if (!(fp = fdopen(fd = dup(fd),mode))) {
  192.         close(fd);
  193.         }
  194.     }
  195.     else {
  196.         while (isSPACE(*name))
  197.         name++;
  198.         if (strEQ(name,"-")) {
  199.         fp = stdout;
  200.         stio->type = '-';
  201.         }
  202.         else  {
  203.         fp = fopen(name,mode);
  204.         }
  205.     }
  206.     }
  207.     else {
  208.     if (*name == '<') {
  209.         mode[0] = 'r';
  210.         name++;
  211.         while (isSPACE(*name))
  212.         name++;
  213.         if (*name == '&')
  214.         goto duplicity;
  215.         if (strEQ(name,"-")) {
  216.         fp = stdin;
  217.         stio->type = '-';
  218.         }
  219.         else
  220.         fp = fopen(name,mode);
  221.     }
  222.     else if (name[len-1] == '|') {
  223. #ifdef TAINT
  224.         taintenv();
  225.         taintproper("Insecure dependency in piped open");
  226. #endif
  227.         name[--len] = '\0';
  228.         while (len && isSPACE(name[len-1]))
  229.         name[--len] = '\0';
  230.         /*SUPPRESS 530*/
  231.         for (; isSPACE(*name); name++) ;
  232.         fp = mypopen(name,"r");
  233.         stio->type = '|';
  234.     }
  235.     else {
  236.         stio->type = '<';
  237.         /*SUPPRESS 530*/
  238.         for (; isSPACE(*name); name++) ;
  239.         if (strEQ(name,"-")) {
  240.         fp = stdin;
  241.         stio->type = '-';
  242.         }
  243.         else
  244.         fp = fopen(name,"r");
  245.     }
  246.     }
  247.     Safefree(myname);
  248.     if (!fp)
  249.     goto say_false;
  250.     if (stio->type &&
  251.       stio->type != '|' && stio->type != '-') {
  252.     if (fstat(fileno(fp),&statbuf) < 0) {
  253.         (void)fclose(fp);
  254.         goto say_false;
  255.     }
  256.     if (S_ISSOCK(statbuf.st_mode))
  257.         stio->type = 's';    /* in case a socket was passed in to us */
  258. #ifdef HAS_SOCKET
  259.     else if (
  260. #ifdef S_IFMT
  261.         !(statbuf.st_mode & S_IFMT)
  262. #else
  263.         !statbuf.st_mode
  264. #endif
  265.     ) {
  266.         if (getsockname(fileno(fp), tokenbuf, 0) >= 0 || errno != ENOTSOCK)
  267.         stio->type = 's'; /* some OS's return 0 on fstat()ed socket */
  268.                 /* but some return 0 for streams too, sigh */
  269.     }
  270. #endif
  271.     }
  272.     if (saveifp) {        /* must use old fp? */
  273.     fd = fileno(saveifp);
  274.     if (saveofp) {
  275.         fflush(saveofp);        /* emulate fclose() */
  276.         if (saveofp != saveifp) {    /* was a socket? */
  277.         fclose(saveofp);
  278.         if (fd > 2)
  279.             Safefree(saveofp);
  280.         }
  281.     }
  282.     if (fd != fileno(fp)) {
  283.         dup2(fileno(fp), fd);
  284.         fclose(fp);
  285.     }
  286.     fp = saveifp;
  287.     }
  288. #if defined(HAS_FCNTL) && defined(F_SETFD)
  289.     fd = fileno(fp);
  290.     fcntl(fd,F_SETFD,fd > maxsysfd);
  291. #endif
  292.     stio->ifp = fp;
  293.     if (writing) {
  294.     if (stio->type == 's'
  295.       || (stio->type == '>' && S_ISCHR(statbuf.st_mode)) ) {
  296.         if (!(stio->ofp = fdopen(fileno(fp),"w"))) {
  297.         fclose(fp);
  298.         stio->ifp = Nullfp;
  299.         goto say_false;
  300.         }
  301.     }
  302.     else
  303.         stio->ofp = fp;
  304.     }
  305.     return TRUE;
  306.  
  307. say_false:
  308.     stio->ifp = saveifp;
  309.     stio->ofp = saveofp;
  310.     stio->type = savetype;
  311.     return FALSE;
  312. }
  313.  
  314. FILE *
  315. nextargv(stab)
  316. register STAB *stab;
  317. {
  318.     register STR *str;
  319. #ifndef FLEXFILENAMES
  320.     int filedev;
  321.     int fileino;
  322. #endif
  323.     int fileuid;
  324.     int filegid;
  325.     static int filemode = 0;
  326.     static int lastfd;
  327.     static char *oldname;
  328.  
  329.     if (!argvoutstab)
  330.     argvoutstab = stabent("ARGVOUT",TRUE);
  331.     if (filemode & (S_ISUID|S_ISGID)) {
  332.     fflush(stab_io(argvoutstab)->ifp);  /* chmod must follow last write */
  333. #ifdef HAS_FCHMOD
  334.     (void)fchmod(lastfd,filemode);
  335. #else
  336.     (void)chmod(oldname,filemode);
  337. #endif
  338.     }
  339.     filemode = 0;
  340.     while (alen(stab_xarray(stab)) >= 0) {
  341.     str = ashift(stab_xarray(stab));
  342.     str_sset(stab_val(stab),str);
  343.     STABSET(stab_val(stab));
  344.     oldname = str_get(stab_val(stab));
  345.     if (do_open(stab,oldname,stab_val(stab)->str_cur)) {
  346.         if (inplace) {
  347. #ifdef TAINT
  348.         taintproper("Insecure dependency in inplace open");
  349. #endif
  350.         if (strEQ(oldname,"-")) {
  351.             str_free(str);
  352.             defoutstab = stabent("STDOUT",TRUE);
  353.             return stab_io(stab)->ifp;
  354.         }
  355. #ifndef FLEXFILENAMES
  356.         filedev = statbuf.st_dev;
  357.         fileino = statbuf.st_ino;
  358. #endif
  359.         filemode = statbuf.st_mode;
  360.         fileuid = statbuf.st_uid;
  361.         filegid = statbuf.st_gid;
  362.         if (!S_ISREG(filemode)) {
  363.             warn("Can't do inplace edit: %s is not a regular file",
  364.               oldname );
  365.             do_close(stab,FALSE);
  366.             str_free(str);
  367.             continue;
  368.         }
  369.         if (*inplace) {
  370. #ifdef SUFFIX
  371.             add_suffix(str,inplace);
  372. #else
  373.             str_cat(str,inplace);
  374. #endif
  375. #ifndef FLEXFILENAMES
  376.             if (stat(str->str_ptr,&statbuf) >= 0
  377.               && statbuf.st_dev == filedev
  378.               && statbuf.st_ino == fileino ) {
  379.             warn("Can't do inplace edit: %s > 14 characters",
  380.               str->str_ptr );
  381.             do_close(stab,FALSE);
  382.             str_free(str);
  383.             continue;
  384.             }
  385. #endif
  386. #ifdef HAS_RENAME
  387. #ifndef MSDOS
  388.             if (rename(oldname,str->str_ptr) < 0) {
  389.             warn("Can't rename %s to %s: %s, skipping file",
  390.               oldname, str->str_ptr, strerror(errno) );
  391.             do_close(stab,FALSE);
  392.             str_free(str);
  393.             continue;
  394.             }
  395. #else
  396.             do_close(stab,FALSE);
  397.             (void)unlink(str->str_ptr);
  398.             (void)rename(oldname,str->str_ptr);
  399.             do_open(stab,str->str_ptr,stab_val(stab)->str_cur);
  400. #endif /* MSDOS */
  401. #else
  402.             (void)UNLINK(str->str_ptr);
  403.             if (link(oldname,str->str_ptr) < 0) {
  404.             warn("Can't rename %s to %s: %s, skipping file",
  405.               oldname, str->str_ptr, strerror(errno) );
  406.             do_close(stab,FALSE);
  407.             str_free(str);
  408.             continue;
  409.             }
  410.             (void)UNLINK(oldname);
  411. #endif
  412.         }
  413.         else {
  414. #ifndef MSDOS
  415.             if (UNLINK(oldname) < 0) {
  416.             warn("Can't rename %s to %s: %s, skipping file",
  417.               oldname, str->str_ptr, strerror(errno) );
  418.             do_close(stab,FALSE);
  419.             str_free(str);
  420.             continue;
  421.             }
  422. #else
  423.             fatal("Can't do inplace edit without backup");
  424. #endif
  425.         }
  426.  
  427.         str_nset(str,">",1);
  428.         str_cat(str,oldname);
  429.         errno = 0;        /* in case sprintf set errno */
  430.         if (!do_open(argvoutstab,str->str_ptr,str->str_cur)) {
  431.             warn("Can't do inplace edit on %s: %s",
  432.               oldname, strerror(errno) );
  433.             do_close(stab,FALSE);
  434.             str_free(str);
  435.             continue;
  436.         }
  437.         defoutstab = argvoutstab;
  438.         lastfd = fileno(stab_io(argvoutstab)->ifp);
  439.         (void)fstat(lastfd,&statbuf);
  440. #ifdef HAS_FCHMOD
  441.         (void)fchmod(lastfd,filemode);
  442. #else
  443.         (void)chmod(oldname,filemode);
  444. #endif
  445.         if (fileuid != statbuf.st_uid || filegid != statbuf.st_gid) {
  446. #ifdef HAS_FCHOWN
  447.             (void)fchown(lastfd,fileuid,filegid);
  448. #else
  449. #ifdef HAS_CHOWN
  450.             (void)chown(oldname,fileuid,filegid);
  451. #endif
  452. #endif
  453.         }
  454.         }
  455.         str_free(str);
  456.         return stab_io(stab)->ifp;
  457.     }
  458.     else
  459. #ifdef macintosh
  460.         fprintf(stderr,"# Can't open %s: %s\n",str_get(str), strerror(errno));
  461. #else
  462.         fprintf(stderr,"Can't open %s: %s\n",str_get(str), strerror(errno));
  463. #endif
  464.     str_free(str);
  465.     }
  466.     if (inplace) {
  467.     (void)do_close(argvoutstab,FALSE);
  468.     defoutstab = stabent("STDOUT",TRUE);
  469.     }
  470.     return Nullfp;
  471. }
  472.  
  473. #ifdef HAS_PIPE 
  474. void
  475. do_pipe(str, rstab, wstab)
  476. STR *str;
  477. STAB *rstab;
  478. STAB *wstab;
  479. {
  480.     register STIO *rstio;
  481.     register STIO *wstio;
  482.     int fd[2];
  483.  
  484.     if (!rstab)
  485.     goto badexit;
  486.     if (!wstab)
  487.     goto badexit;
  488.  
  489.     rstio = stab_io(rstab);
  490.     wstio = stab_io(wstab);
  491.  
  492.     if (!rstio)
  493.     rstio = stab_io(rstab) = stio_new();
  494.     else if (rstio->ifp)
  495.     do_close(rstab,FALSE);
  496.     if (!wstio)
  497.     wstio = stab_io(wstab) = stio_new();
  498.     else if (wstio->ifp)
  499.     do_close(wstab,FALSE);
  500.  
  501.     if (pipe(fd) < 0)
  502.     goto badexit;
  503.     rstio->ifp = fdopen(fd[0], "r");
  504.     wstio->ofp = fdopen(fd[1], "w");
  505.     wstio->ifp = wstio->ofp;
  506.     rstio->type = '<';
  507.     wstio->type = '>';
  508.     if (!rstio->ifp || !wstio->ofp) {
  509.     if (rstio->ifp) fclose(rstio->ifp);
  510.     else close(fd[0]);
  511.     if (wstio->ofp) fclose(wstio->ofp);
  512.     else close(fd[1]);
  513.     goto badexit;
  514.     }
  515.  
  516.     str_sset(str,&str_yes);
  517.     return;
  518.  
  519. badexit:
  520.     str_sset(str,&str_undef);
  521.     return;
  522. }
  523. #endif
  524.  
  525. bool
  526. do_close(stab,explicit)
  527. STAB *stab;
  528. bool explicit;
  529. {
  530.     bool retval = FALSE;
  531.     register STIO *stio;
  532.     int status;
  533.  
  534.     if (!stab)
  535.     stab = argvstab;
  536.     if (!stab) {
  537.     errno = EBADF;
  538.     return FALSE;
  539.     }
  540.     stio = stab_io(stab);
  541.     if (!stio) {        /* never opened */
  542.     if (dowarn && explicit)
  543.         warn("Close on unopened file <%s>",stab_name(stab));
  544.     return FALSE;
  545.     }
  546.     if (stio->ifp) {
  547.     if (stio->type == '|') {
  548.         status = mypclose(stio->ifp);
  549.         retval = (status == 0);
  550.         statusvalue = (unsigned short)status & 0xffff;
  551.     }
  552.     else if (stio->type == '-')
  553.         retval = TRUE;
  554.     else {
  555.         if (stio->ofp && stio->ofp != stio->ifp) {        /* a socket */
  556.         retval = (fclose(stio->ofp) != EOF);
  557.         fclose(stio->ifp);    /* clear stdio, fd already closed */
  558.         }
  559.         else
  560.         retval = (fclose(stio->ifp) != EOF);
  561.     }
  562.     stio->ofp = stio->ifp = Nullfp;
  563.     }
  564.     if (explicit)
  565.     stio->lines = 0;
  566.     stio->type = ' ';
  567.     return retval;
  568. }
  569.  
  570. bool
  571. do_eof(stab)
  572. STAB *stab;
  573. {
  574.     register STIO *stio;
  575.     int ch;
  576.  
  577.     if (!stab) {            /* eof() */
  578.     if (argvstab)
  579.         stio = stab_io(argvstab);
  580.     else
  581.         return TRUE;
  582.     }
  583.     else
  584.     stio = stab_io(stab);
  585.  
  586.     if (!stio)
  587.     return TRUE;
  588.  
  589.     while (stio->ifp) {
  590.  
  591. #ifdef STDSTDIO            /* (the code works without this) */
  592.     if (stio->ifp->_cnt > 0)    /* cheat a little, since */
  593.         return FALSE;        /* this is the most usual case */
  594. #endif
  595.  
  596.     ch = getc(stio->ifp);
  597.     if (ch != EOF) {
  598.         (void)ungetc(ch, stio->ifp);
  599.         return FALSE;
  600.     }
  601. #ifdef STDSTDIO
  602.     if (stio->ifp->_cnt < -1)
  603.         stio->ifp->_cnt = -1;
  604. #endif
  605.     if (!stab) {            /* not necessarily a real EOF yet? */
  606.         if (!nextargv(argvstab))    /* get another fp handy */
  607.         return TRUE;
  608.     }
  609.     else
  610.         return TRUE;        /* normal fp, definitely end of file */
  611.     }
  612.     return TRUE;
  613. }
  614.  
  615. long
  616. do_tell(stab)
  617. STAB *stab;
  618. {
  619.     register STIO *stio;
  620.  
  621.     if (!stab)
  622.     goto phooey;
  623.  
  624.     stio = stab_io(stab);
  625.     if (!stio || !stio->ifp)
  626.     goto phooey;
  627.  
  628.     if (feof(stio->ifp))
  629.     (void)fseek (stio->ifp, 0L, 2);        /* ultrix 1.2 workaround */
  630.  
  631.     return ftell(stio->ifp);
  632.  
  633. phooey:
  634.     if (dowarn)
  635.     warn("tell() on unopened file");
  636.     errno = EBADF;
  637.     return -1L;
  638. }
  639.  
  640. bool
  641. do_seek(stab, pos, whence)
  642. STAB *stab;
  643. long pos;
  644. int whence;
  645. {
  646.     register STIO *stio;
  647.  
  648.     if (!stab)
  649.     goto nuts;
  650.  
  651.     stio = stab_io(stab);
  652.     if (!stio || !stio->ifp)
  653.     goto nuts;
  654.  
  655.     if (feof(stio->ifp))
  656.     (void)fseek (stio->ifp, 0L, 2);        /* ultrix 1.2 workaround */
  657.  
  658.     return fseek(stio->ifp, pos, whence) >= 0;
  659.  
  660. nuts:
  661.     if (dowarn)
  662.     warn("seek() on unopened file");
  663.     errno = EBADF;
  664.     return FALSE;
  665. }
  666.  
  667. int
  668. do_ctl(optype,stab,func,argstr)
  669. int optype;
  670. STAB *stab;
  671. int func;
  672. STR *argstr;
  673. {
  674.     register STIO *stio;
  675.     register char *s;
  676.     int retval;
  677.  
  678.     if (!stab || !argstr || !(stio = stab_io(stab)) || !stio->ifp) {
  679.     errno = EBADF;    /* well, sort of... */
  680.     return -1;
  681.     }
  682.  
  683.     if (argstr->str_pok || !argstr->str_nok) {
  684.     if (!argstr->str_pok)
  685.         s = str_get(argstr);
  686.  
  687. #ifdef IOCPARM_MASK
  688. #ifndef IOCPARM_LEN
  689. #define IOCPARM_LEN(x)  (((x) >> 16) & IOCPARM_MASK)
  690. #endif
  691. #endif
  692. #ifdef IOCPARM_LEN
  693.     retval = IOCPARM_LEN(func);    /* on BSDish systes we're safe */
  694. #else
  695.     retval = 256;            /* otherwise guess at what's safe */
  696. #endif
  697.     if (argstr->str_cur < retval) {
  698.         Str_Grow(argstr,retval+1);
  699.         argstr->str_cur = retval;
  700.     }
  701.  
  702.     s = argstr->str_ptr;
  703.     s[argstr->str_cur] = 17;    /* a little sanity check here */
  704.     }
  705.     else {
  706.     retval = (int)str_gnum(argstr);
  707. #ifdef MSDOS
  708.     s = (char*)(long)retval;        /* ouch */
  709. #else
  710.     s = (char*)retval;        /* ouch */
  711. #endif
  712.     }
  713.  
  714. #ifndef lint
  715.     if (optype == O_IOCTL)
  716.     retval = ioctl(fileno(stio->ifp), func, s);
  717.     else
  718. #ifdef MSDOS
  719.     fatal("fcntl is not implemented");
  720. #else
  721. #ifdef macintosh
  722.     retval = fcntl(fileno(stio->ifp), func, (int) s);
  723. #else
  724. #ifdef HAS_FCNTL
  725.     retval = fcntl(fileno(stio->ifp), func, s);
  726. #else
  727.     fatal("fcntl is not implemented");
  728. #endif
  729. #endif
  730. #endif
  731. #else /* lint */
  732.     retval = 0;
  733. #endif /* lint */
  734.  
  735.     if (argstr->str_pok) {
  736.     if (s[argstr->str_cur] != 17)
  737.         fatal("Return value overflowed string");
  738.     s[argstr->str_cur] = 0;        /* put our null back */
  739.     }
  740.     return retval;
  741. }
  742.  
  743. int
  744. do_stat(str,arg,gimme,arglast)
  745. STR *str;
  746. register ARG *arg;
  747. int gimme;
  748. int *arglast;
  749. {
  750.     register ARRAY *ary = stack;
  751.     register int sp = arglast[0] + 1;
  752.     int max = 13;
  753.  
  754.     if ((arg[1].arg_type & A_MASK) == A_WORD) {
  755.     tmpstab = arg[1].arg_ptr.arg_stab;
  756.     if (tmpstab != defstab) {
  757.         laststype = O_STAT;
  758.         statstab = tmpstab;
  759.         str_set(statname,"");
  760.         if (!stab_io(tmpstab) || !stab_io(tmpstab)->ifp ||
  761.           fstat(fileno(stab_io(tmpstab)->ifp),&statcache) < 0) {
  762.         max = 0;
  763.         laststatval = -1;
  764.         }
  765.     }
  766.     else if (laststatval < 0)
  767.         max = 0;
  768.     }
  769.     else {
  770.     str_set(statname,str_get(ary->ary_array[sp]));
  771.     statstab = Nullstab;
  772. #ifdef HAS_LSTAT
  773.     laststype = arg->arg_type;
  774.     if (arg->arg_type == O_LSTAT)
  775.         laststatval = lstat(str_get(statname),&statcache);
  776.     else
  777. #endif
  778.         laststatval = stat(str_get(statname),&statcache);
  779.     if (laststatval < 0)
  780.         max = 0;
  781.     }
  782.  
  783.     if (gimme != G_ARRAY) {
  784.     if (max)
  785.         str_sset(str,&str_yes);
  786.     else
  787.         str_sset(str,&str_undef);
  788.     STABSET(str);
  789.     ary->ary_array[sp] = str;
  790.     return sp;
  791.     }
  792.     sp--;
  793.     if (max) {
  794. #ifndef lint
  795.     (void)astore(ary,++sp,
  796.       str_2mortal(str_nmake((double)statcache.st_dev)));
  797.     (void)astore(ary,++sp,
  798.       str_2mortal(str_nmake((double)statcache.st_ino)));
  799.     (void)astore(ary,++sp,
  800.       str_2mortal(str_nmake((double)statcache.st_mode)));
  801.     (void)astore(ary,++sp,
  802.       str_2mortal(str_nmake((double)statcache.st_nlink)));
  803.     (void)astore(ary,++sp,
  804.       str_2mortal(str_nmake((double)statcache.st_uid)));
  805.     (void)astore(ary,++sp,
  806.       str_2mortal(str_nmake((double)statcache.st_gid)));
  807.     (void)astore(ary,++sp,
  808.       str_2mortal(str_nmake((double)statcache.st_rdev)));
  809.     (void)astore(ary,++sp,
  810.       str_2mortal(str_nmake((double)statcache.st_size)));
  811.     (void)astore(ary,++sp,
  812.       str_2mortal(str_nmake((double)statcache.st_atime)));
  813.     (void)astore(ary,++sp,
  814.       str_2mortal(str_nmake((double)statcache.st_mtime)));
  815.     (void)astore(ary,++sp,
  816.       str_2mortal(str_nmake((double)statcache.st_ctime)));
  817. #ifdef STATBLOCKS
  818.     (void)astore(ary,++sp,
  819.       str_2mortal(str_nmake((double)statcache.st_blksize)));
  820.     (void)astore(ary,++sp,
  821.       str_2mortal(str_nmake((double)statcache.st_blocks)));
  822. #else
  823.     (void)astore(ary,++sp,
  824.       str_2mortal(str_make("",0)));
  825.     (void)astore(ary,++sp,
  826.       str_2mortal(str_make("",0)));
  827. #endif
  828. #else /* lint */
  829.     (void)astore(ary,++sp,str_nmake(0.0));
  830. #endif /* lint */
  831.     }
  832.     return sp;
  833. }
  834.  
  835. #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
  836.     /* code courtesy of William Kucharski */
  837. #define HAS_CHSIZE
  838.  
  839. int chsize(fd, length)
  840. int fd;            /* file descriptor */
  841. off_t length;        /* length to set file to */
  842. {
  843.     extern long lseek();
  844.     struct flock fl;
  845.     struct stat filebuf;
  846.  
  847.     if (fstat(fd, &filebuf) < 0)
  848.     return -1;
  849.  
  850.     if (filebuf.st_size < length) {
  851.  
  852.     /* extend file length */
  853.  
  854.     if ((lseek(fd, (length - 1), 0)) < 0)
  855.         return -1;
  856.  
  857.     /* write a "0" byte */
  858.  
  859.     if ((write(fd, "", 1)) != 1)
  860.         return -1;
  861.     }
  862.     else {
  863.     /* truncate length */
  864.  
  865.     fl.l_whence = 0;
  866.     fl.l_len = 0;
  867.     fl.l_start = length;
  868.     fl.l_type = F_WRLCK;    /* write lock on file space */
  869.  
  870.     /*
  871.     * This relies on the UNDOCUMENTED F_FREESP argument to
  872.     * fcntl(2), which truncates the file so that it ends at the
  873.     * position indicated by fl.l_start.
  874.     *
  875.     * Will minor miracles never cease?
  876.     */
  877.  
  878.     if (fcntl(fd, F_FREESP, &fl) < 0)
  879.         return -1;
  880.  
  881.     }
  882.  
  883.     return 0;
  884. }
  885. #endif /* F_FREESP */
  886.  
  887. int                    /*SUPPRESS 590*/
  888. do_truncate(str,arg,gimme,arglast)
  889. STR *str;
  890. register ARG *arg;
  891. int gimme;
  892. int *arglast;
  893. {
  894.     register ARRAY *ary = stack;
  895.     register int sp = arglast[0] + 1;
  896.     off_t len = (off_t)str_gnum(ary->ary_array[sp+1]);
  897.     int result = 1;
  898.     STAB *tmpstab;
  899.  
  900. #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE)
  901. #ifdef HAS_TRUNCATE
  902.     if ((arg[1].arg_type & A_MASK) == A_WORD) {
  903.     tmpstab = arg[1].arg_ptr.arg_stab;
  904.     if (!stab_io(tmpstab) || !stab_io(tmpstab)->ifp ||
  905.       ftruncate(fileno(stab_io(tmpstab)->ifp), len) < 0)
  906.         result = 0;
  907.     }
  908.     else if (truncate(str_get(ary->ary_array[sp]), len) < 0)
  909.     result = 0;
  910. #else
  911.     if ((arg[1].arg_type & A_MASK) == A_WORD) {
  912.     tmpstab = arg[1].arg_ptr.arg_stab;
  913.     if (!stab_io(tmpstab) || !stab_io(tmpstab)->ifp ||
  914.       chsize(fileno(stab_io(tmpstab)->ifp), len) < 0)
  915.         result = 0;
  916.     }
  917.     else {
  918.     int tmpfd;
  919.  
  920.     if ((tmpfd = open(str_get(ary->ary_array[sp]), 0)) < 0)
  921.         result = 0;
  922.     else {
  923.         if (chsize(tmpfd, len) < 0)
  924.         result = 0;
  925.         close(tmpfd);
  926.     }
  927.     }
  928. #endif
  929.  
  930.     if (result)
  931.     str_sset(str,&str_yes);
  932.     else
  933.     str_sset(str,&str_undef);
  934.     STABSET(str);
  935.     ary->ary_array[sp] = str;
  936.     return sp;
  937. #else
  938.     fatal("truncate not implemented");
  939. #endif
  940. }
  941.  
  942. int
  943. looks_like_number(str)
  944. STR *str;
  945. {
  946.     register char *s;
  947.     register char *send;
  948.  
  949.     if (!str->str_pok)
  950.     return TRUE;
  951.     s = str->str_ptr; 
  952.     send = s + str->str_cur;
  953.     while (isSPACE(*s))
  954.     s++;
  955.     if (s >= send)
  956.     return FALSE;
  957.     if (*s == '+' || *s == '-')
  958.     s++;
  959.     while (isDIGIT(*s))
  960.     s++;
  961.     if (s == send)
  962.     return TRUE;
  963.     if (*s == '.') 
  964.     s++;
  965.     else if (s == str->str_ptr)
  966.     return FALSE;
  967.     while (isDIGIT(*s))
  968.     s++;
  969.     if (s == send)
  970.     return TRUE;
  971.     if (*s == 'e' || *s == 'E') {
  972.     s++;
  973.     if (*s == '+' || *s == '-')
  974.         s++;
  975.     while (isDIGIT(*s))
  976.         s++;
  977.     }
  978.     while (isSPACE(*s))
  979.     s++;
  980.     if (s >= send)
  981.     return TRUE;
  982.     return FALSE;
  983. }
  984.  
  985. bool
  986. do_print(str,fp)
  987. register STR *str;
  988. FILE *fp;
  989. {
  990.     register char *tmps;
  991.  
  992.     if (!fp) {
  993.     if (dowarn)
  994.         warn("print to unopened file");
  995.     errno = EBADF;
  996.     return FALSE;
  997.     }
  998.     if (!str)
  999.     return TRUE;
  1000.     if (ofmt &&
  1001.       ((str->str_nok && str->str_u.str_nval != 0.0)
  1002.        || (looks_like_number(str) && str_gnum(str) != 0.0) ) ) {
  1003.     fprintf(fp, ofmt, str->str_u.str_nval);
  1004.     return !ferror(fp);
  1005.     }
  1006.     else {
  1007.     tmps = str_get(str);
  1008.     if (*tmps == 'S' && tmps[1] == 't' && tmps[2] == 'B' && tmps[3] == '\0'
  1009.       && str->str_cur == sizeof(STBP) && strlen(tmps) < str->str_cur) {
  1010.         STR *tmpstr = str_mortal(&str_undef);
  1011.         stab_fullname(tmpstr,((STAB*)str));/* a stab value, be nice */
  1012.         str = tmpstr;
  1013.         tmps = str->str_ptr;
  1014.         putc('*',fp);
  1015.     }
  1016.     if (str->str_cur && (fwrite(tmps,1,str->str_cur,fp) == 0 || ferror(fp)))
  1017.         return FALSE;
  1018.     }
  1019.     return TRUE;
  1020. }
  1021.  
  1022. bool
  1023. do_aprint(arg,fp,arglast)
  1024. register ARG *arg;
  1025. register FILE *fp;
  1026. int *arglast;
  1027. {
  1028.     register STR **st = stack->ary_array;
  1029.     register int sp = arglast[1];
  1030.     register int retval;
  1031.     register int items = arglast[2] - sp;
  1032.  
  1033.     if (!fp) {
  1034.     if (dowarn)
  1035.         warn("print to unopened file");
  1036.     errno = EBADF;
  1037.     return FALSE;
  1038.     }
  1039.     st += ++sp;
  1040.     if (arg->arg_type == O_PRTF) {
  1041.     do_sprintf(arg->arg_ptr.arg_str,items,st);
  1042.     retval = do_print(arg->arg_ptr.arg_str,fp);
  1043.     }
  1044.     else {
  1045.     retval = (items <= 0);
  1046.     for (; items > 0; items--,st++) {
  1047.         if (retval && ofslen) {
  1048.         if (fwrite(ofs, 1, ofslen, fp) == 0 || ferror(fp)) {
  1049.             retval = FALSE;
  1050.             break;
  1051.         }
  1052.         }
  1053.         if (!(retval = do_print(*st, fp)))
  1054.         break;
  1055.     }
  1056.     if (retval && orslen)
  1057.         if (fwrite(ors, 1, orslen, fp) == 0 || ferror(fp))
  1058.         retval = FALSE;
  1059.     }
  1060.     return retval;
  1061. }
  1062.  
  1063. int
  1064. mystat(arg,str)
  1065. ARG *arg;
  1066. STR *str;
  1067. {
  1068.     STIO *stio;
  1069.  
  1070.     if (arg[1].arg_type & A_DONT) {
  1071.     stio = stab_io(arg[1].arg_ptr.arg_stab);
  1072.     if (stio && stio->ifp) {
  1073.         statstab = arg[1].arg_ptr.arg_stab;
  1074.         str_set(statname,"");
  1075.         laststype = O_STAT;
  1076.         return (laststatval = fstat(fileno(stio->ifp), &statcache));
  1077.     }
  1078.     else {
  1079.         if (arg[1].arg_ptr.arg_stab == defstab)
  1080.         return laststatval;
  1081.         if (dowarn)
  1082.         warn("Stat on unopened file <%s>",
  1083.           stab_name(arg[1].arg_ptr.arg_stab));
  1084.         statstab = Nullstab;
  1085.         str_set(statname,"");
  1086.         return (laststatval = -1);
  1087.     }
  1088.     }
  1089.     else {
  1090.     statstab = Nullstab;
  1091.     str_set(statname,str_get(str));
  1092.     laststype = O_STAT;
  1093.     return (laststatval = stat(str_get(str),&statcache));
  1094.     }
  1095. }
  1096.  
  1097. int
  1098. mylstat(arg,str)
  1099. ARG *arg;
  1100. STR *str;
  1101. {
  1102.     if (arg[1].arg_type & A_DONT) {
  1103.     if (arg[1].arg_ptr.arg_stab == defstab) {
  1104.         if (laststype != O_LSTAT)
  1105.         fatal("The stat preceding -l _ wasn't an lstat");
  1106.         return laststatval;
  1107.     }
  1108.     fatal("You can't use -l on a filehandle");
  1109.     }
  1110.  
  1111.     laststype = O_LSTAT;
  1112.     statstab = Nullstab;
  1113.     str_set(statname,str_get(str));
  1114. #ifdef HAS_LSTAT
  1115.     return (laststatval = lstat(str_get(str),&statcache));
  1116. #else
  1117.     return (laststatval = stat(str_get(str),&statcache));
  1118. #endif
  1119. }
  1120.  
  1121. STR *
  1122. do_fttext(arg,str)
  1123. register ARG *arg;
  1124. STR *str;
  1125. {
  1126.     int i;
  1127.     int len;
  1128.     int odd = 0;
  1129.     STDCHAR tbuf[512];
  1130.     register STDCHAR *s;
  1131.     register STIO *stio;
  1132.  
  1133.     if (arg[1].arg_type & A_DONT) {
  1134.     if (arg[1].arg_ptr.arg_stab == defstab) {
  1135.         if (statstab)
  1136.         stio = stab_io(statstab);
  1137.         else {
  1138.         str = statname;
  1139.         goto really_filename;
  1140.         }
  1141.     }
  1142.     else {
  1143.         statstab = arg[1].arg_ptr.arg_stab;
  1144.         str_set(statname,"");
  1145.         stio = stab_io(statstab);
  1146.     }
  1147.     if (stio && stio->ifp) {
  1148. #ifdef STDSTDIO
  1149.         fstat(fileno(stio->ifp),&statcache);
  1150.         if (S_ISDIR(statcache.st_mode))    /* handle NFS glitch */
  1151.         return arg->arg_type == O_FTTEXT ? &str_no : &str_yes;
  1152.         if (stio->ifp->_cnt <= 0) {
  1153.         i = getc(stio->ifp);
  1154.         if (i != EOF)
  1155.             (void)ungetc(i,stio->ifp);
  1156.         }
  1157.         if (stio->ifp->_cnt <= 0)    /* null file is anything */
  1158.         return &str_yes;
  1159.         len = stio->ifp->_cnt + (stio->ifp->_ptr - stio->ifp->_base);
  1160.         s = stio->ifp->_base;
  1161. #else
  1162.         fatal("-T and -B not implemented on filehandles");
  1163. #endif
  1164.     }
  1165.     else {
  1166.         if (dowarn)
  1167.         warn("Test on unopened file <%s>",
  1168.           stab_name(arg[1].arg_ptr.arg_stab));
  1169.         errno = EBADF;
  1170.         return &str_undef;
  1171.     }
  1172.     }
  1173.     else {
  1174.     statstab = Nullstab;
  1175.     str_set(statname,str_get(str));
  1176.       really_filename:
  1177.     i = open(str_get(str),0);
  1178.     if (i < 0)
  1179.         return &str_undef;
  1180.     fstat(i,&statcache);
  1181.     len = read(i,tbuf,512);
  1182.     (void)close(i);
  1183.     if (len <= 0) {
  1184.         if (S_ISDIR(statcache.st_mode) && arg->arg_type == O_FTTEXT)
  1185.         return &str_no;        /* special case NFS directories */
  1186.         return &str_yes;        /* null file is anything */
  1187.     }
  1188.     s = tbuf;
  1189.     }
  1190.  
  1191.     /* now scan s to look for textiness */
  1192.  
  1193.     for (i = 0; i < len; i++,s++) {
  1194.     if (!*s) {            /* null never allowed in text */
  1195.         odd += len;
  1196.         break;
  1197.     }
  1198.     else if (*s & 128)
  1199.         odd++;
  1200.     else if (*s < 32 &&
  1201.       *s != '\n' && *s != '\r' && *s != '\b' &&
  1202.       *s != '\t' && *s != '\f' && *s != 27)
  1203.         odd++;
  1204.     }
  1205.  
  1206.     if ((odd * 10 > len) == (arg->arg_type == O_FTTEXT)) /* allow 10% odd */
  1207.     return &str_no;
  1208.     else
  1209.     return &str_yes;
  1210. }
  1211.  
  1212. bool
  1213. do_aexec(really,arglast)
  1214. STR *really;
  1215. int *arglast;
  1216. {
  1217.     register STR **st = stack->ary_array;
  1218.     register int sp = arglast[1];
  1219.     register int items = arglast[2] - sp;
  1220.     register char **a;
  1221.     char **argv;
  1222.     char *tmps;
  1223.  
  1224.     if (items) {
  1225.     New(401,argv, items+1, char*);
  1226.     a = argv;
  1227.     for (st += ++sp; items > 0; items--,st++) {
  1228.         if (*st)
  1229.         *a++ = str_get(*st);
  1230.         else
  1231.         *a++ = "";
  1232.     }
  1233.     *a = Nullch;
  1234. #ifdef TAINT
  1235.     if (*argv[0] != '/')    /* will execvp use PATH? */
  1236.         taintenv();        /* testing IFS here is overkill, probably */
  1237. #endif
  1238.     if (really && *(tmps = str_get(really)))
  1239.         execvp(tmps,argv);
  1240.     else
  1241.         execvp(argv[0],argv);
  1242.     Safefree(argv);
  1243.     }
  1244.     return FALSE;
  1245. }
  1246.  
  1247. static char **Argv = Null(char **);
  1248. static char *Cmd = Nullch;
  1249.  
  1250. void
  1251. do_execfree()
  1252. {
  1253.     if (Argv) {
  1254.     Safefree(Argv);
  1255.     Argv = Null(char **);
  1256.     }
  1257.     if (Cmd) {
  1258.     Safefree(Cmd);
  1259.     Cmd = Nullch;
  1260.     }
  1261. }
  1262.  
  1263. bool
  1264. do_exec(cmd)
  1265. char *cmd;
  1266. {
  1267. #ifdef macintosh
  1268.     fatal("exec() on a mac ? You gotta be joking !");
  1269. #else
  1270.     register char **a;
  1271.     register char *s;
  1272.     char flags[10];
  1273.  
  1274.     /* save an extra exec if possible */
  1275.  
  1276. #ifdef CSH
  1277.     if (strnEQ(cmd,cshname,cshlen) && strnEQ(cmd+cshlen," -c",3)) {
  1278.     strcpy(flags,"-c");
  1279.     s = cmd+cshlen+3;
  1280.     if (*s == 'f') {
  1281.         s++;
  1282.         strcat(flags,"f");
  1283.     }
  1284.     if (*s == ' ')
  1285.         s++;
  1286.     if (*s++ == '\'') {
  1287.         char *ncmd = s;
  1288.  
  1289.         while (*s)
  1290.         s++;
  1291.         if (s[-1] == '\n')
  1292.         *--s = '\0';
  1293.         if (s[-1] == '\'') {
  1294.         *--s = '\0';
  1295.         execl(cshname,"csh", flags,ncmd,(char*)0);
  1296.         *s = '\'';
  1297.         return FALSE;
  1298.         }
  1299.     }
  1300.     }
  1301. #endif /* CSH */
  1302.  
  1303.     /* see if there are shell metacharacters in it */
  1304.  
  1305.     /*SUPPRESS 530*/
  1306.     for (s = cmd; *s && isALPHA(*s); s++) ;    /* catch VAR=val gizmo */
  1307.     if (*s == '=')
  1308.     goto doshell;
  1309.     for (s = cmd; *s; s++) {
  1310.     if (*s != ' ' && !isALPHA(*s) && index("$&*(){}[]'\";\\|?<>~`\n",*s)) {
  1311.         if (*s == '\n' && !s[1]) {
  1312.         *s = '\0';
  1313.         break;
  1314.         }
  1315.       doshell:
  1316.         execl("/bin/sh","sh","-c",cmd,(char*)0);
  1317.         return FALSE;
  1318.     }
  1319.     }
  1320.     New(402,Argv, (s - cmd) / 2 + 2, char*);
  1321.     Cmd = nsavestr(cmd, s-cmd);
  1322.     a = Argv;
  1323.     for (s = Cmd; *s;) {
  1324.     while (*s && isSPACE(*s)) s++;
  1325.     if (*s)
  1326.         *(a++) = s;
  1327.     while (*s && !isSPACE(*s)) s++;
  1328.     if (*s)
  1329.         *s++ = '\0';
  1330.     }
  1331.     *a = Nullch;
  1332.     if (Argv[0]) {
  1333.     execvp(Argv[0],Argv);
  1334.     if (errno == ENOEXEC) {        /* for system V NIH syndrome */
  1335.         do_execfree();
  1336.         goto doshell;
  1337.     }
  1338.     }
  1339.     do_execfree();
  1340.     return FALSE;
  1341. #endif
  1342. }
  1343.  
  1344. #ifdef HAS_SOCKET
  1345. int
  1346. do_socket(stab, arglast)
  1347. STAB *stab;
  1348. int *arglast;
  1349. {
  1350.     register STR **st = stack->ary_array;
  1351.     register int sp = arglast[1];
  1352.     register STIO *stio;
  1353.     int domain, type, protocol, fd;
  1354.  
  1355.     if (!stab) {
  1356.     errno = EBADF;
  1357.     return FALSE;
  1358.     }
  1359.  
  1360.     stio = stab_io(stab);
  1361.     if (!stio)
  1362.     stio = stab_io(stab) = stio_new();
  1363.     else if (stio->ifp)
  1364.     do_close(stab,FALSE);
  1365.  
  1366.     domain = (int)str_gnum(st[++sp]);
  1367.     type = (int)str_gnum(st[++sp]);
  1368.     protocol = (int)str_gnum(st[++sp]);
  1369. #ifdef TAINT
  1370.     taintproper("Insecure dependency in socket");
  1371. #endif
  1372.     fd = socket(domain,type,protocol);
  1373.     if (fd < 0)
  1374.     return FALSE;
  1375.     stio->ifp = fdopen(fd, "r");    /* stdio gets confused about sockets */
  1376.     stio->ofp = fdopen(fd, "w");
  1377.     stio->type = 's';
  1378.     if (!stio->ifp || !stio->ofp) {
  1379.     if (stio->ifp) fclose(stio->ifp);
  1380.     if (stio->ofp) fclose(stio->ofp);
  1381.     if (!stio->ifp && !stio->ofp) close(fd);
  1382.     return FALSE;
  1383.     }
  1384.  
  1385.     return TRUE;
  1386. }
  1387.  
  1388. int
  1389. do_bind(stab, arglast)
  1390. STAB *stab;
  1391. int *arglast;
  1392. {
  1393.     register STR **st = stack->ary_array;
  1394.     register int sp = arglast[1];
  1395.     register STIO *stio;
  1396.     char *addr;
  1397.  
  1398.     if (!stab)
  1399.     goto nuts;
  1400.  
  1401.     stio = stab_io(stab);
  1402.     if (!stio || !stio->ifp)
  1403.     goto nuts;
  1404.  
  1405.     addr = str_get(st[++sp]);
  1406. #ifdef TAINT
  1407.     taintproper("Insecure dependency in bind");
  1408. #endif
  1409.     return bind(fileno(stio->ifp), addr, st[sp]->str_cur) >= 0;
  1410.  
  1411. nuts:
  1412.     if (dowarn)
  1413.     warn("bind() on closed fd");
  1414.     errno = EBADF;
  1415.     return FALSE;
  1416.  
  1417. }
  1418.  
  1419. int
  1420. do_connect(stab, arglast)
  1421. STAB *stab;
  1422. int *arglast;
  1423. {
  1424.     register STR **st = stack->ary_array;
  1425.     register int sp = arglast[1];
  1426.     register STIO *stio;
  1427.     char *addr;
  1428.  
  1429.     if (!stab)
  1430.     goto nuts;
  1431.  
  1432.     stio = stab_io(stab);
  1433.     if (!stio || !stio->ifp)
  1434.     goto nuts;
  1435.  
  1436.     addr = str_get(st[++sp]);
  1437. #ifdef TAINT
  1438.     taintproper("Insecure dependency in connect");
  1439. #endif
  1440.     return connect(fileno(stio->ifp), addr, st[sp]->str_cur) >= 0;
  1441.  
  1442. nuts:
  1443.     if (dowarn)
  1444.     warn("connect() on closed fd");
  1445.     errno = EBADF;
  1446.     return FALSE;
  1447.  
  1448. }
  1449.  
  1450. int
  1451. do_listen(stab, arglast)
  1452. STAB *stab;
  1453. int *arglast;
  1454. {
  1455.     register STR **st = stack->ary_array;
  1456.     register int sp = arglast[1];
  1457.     register STIO *stio;
  1458.     int backlog;
  1459.  
  1460.     if (!stab)
  1461.     goto nuts;
  1462.  
  1463.     stio = stab_io(stab);
  1464.     if (!stio || !stio->ifp)
  1465.     goto nuts;
  1466.  
  1467.     backlog = (int)str_gnum(st[++sp]);
  1468.     return listen(fileno(stio->ifp), backlog) >= 0;
  1469.  
  1470. nuts:
  1471.     if (dowarn)
  1472.     warn("listen() on closed fd");
  1473.     errno = EBADF;
  1474.     return FALSE;
  1475. }
  1476.  
  1477. void
  1478. do_accept(str, nstab, gstab)
  1479. STR *str;
  1480. STAB *nstab;
  1481. STAB *gstab;
  1482. {
  1483.     register STIO *nstio;
  1484.     register STIO *gstio;
  1485.     int len = sizeof buf;
  1486.     int fd;
  1487.  
  1488.     if (!nstab)
  1489.     goto badexit;
  1490.     if (!gstab)
  1491.     goto nuts;
  1492.  
  1493.     gstio = stab_io(gstab);
  1494.     nstio = stab_io(nstab);
  1495.  
  1496.     if (!gstio || !gstio->ifp)
  1497.     goto nuts;
  1498.     if (!nstio)
  1499.     nstio = stab_io(nstab) = stio_new();
  1500.     else if (nstio->ifp)
  1501.     do_close(nstab,FALSE);
  1502.  
  1503.     fd = accept(fileno(gstio->ifp),(struct sockaddr *)buf,&len);
  1504.     if (fd < 0)
  1505.     goto badexit;
  1506.     nstio->ifp = fdopen(fd, "r");
  1507.     nstio->ofp = fdopen(fd, "w");
  1508.     nstio->type = 's';
  1509.     if (!nstio->ifp || !nstio->ofp) {
  1510.     if (nstio->ifp) fclose(nstio->ifp);
  1511.     if (nstio->ofp) fclose(nstio->ofp);
  1512.     if (!nstio->ifp && !nstio->ofp) close(fd);
  1513.     goto badexit;
  1514.     }
  1515.  
  1516.     str_nset(str, buf, len);
  1517.     return;
  1518.  
  1519. nuts:
  1520.     if (dowarn)
  1521.     warn("accept() on closed fd");
  1522.     errno = EBADF;
  1523. badexit:
  1524.     str_sset(str,&str_undef);
  1525.     return;
  1526. }
  1527.  
  1528. int
  1529. do_shutdown(stab, arglast)
  1530. STAB *stab;
  1531. int *arglast;
  1532. {
  1533.     register STR **st = stack->ary_array;
  1534.     register int sp = arglast[1];
  1535.     register STIO *stio;
  1536.     int how;
  1537.  
  1538.     if (!stab)
  1539.     goto nuts;
  1540.  
  1541.     stio = stab_io(stab);
  1542.     if (!stio || !stio->ifp)
  1543.     goto nuts;
  1544.  
  1545.     how = (int)str_gnum(st[++sp]);
  1546.     return shutdown(fileno(stio->ifp), how) >= 0;
  1547.  
  1548. nuts:
  1549.     if (dowarn)
  1550.     warn("shutdown() on closed fd");
  1551.     errno = EBADF;
  1552.     return FALSE;
  1553.  
  1554. }
  1555.  
  1556. int
  1557. do_sopt(optype, stab, arglast)
  1558. int optype;
  1559. STAB *stab;
  1560. int *arglast;
  1561. {
  1562.     register STR **st = stack->ary_array;
  1563.     register int sp = arglast[1];
  1564.     register STIO *stio;
  1565.     int fd;
  1566.     int lvl;
  1567.     int optname;
  1568.  
  1569.     if (!stab)
  1570.     goto nuts;
  1571.  
  1572.     stio = stab_io(stab);
  1573.     if (!stio || !stio->ifp)
  1574.     goto nuts;
  1575.  
  1576.     fd = fileno(stio->ifp);
  1577.     lvl = (int)str_gnum(st[sp+1]);
  1578.     optname = (int)str_gnum(st[sp+2]);
  1579.     switch (optype) {
  1580.     case O_GSOCKOPT:
  1581.     st[sp] = str_2mortal(Str_new(22,257));
  1582.     st[sp]->str_cur = 256;
  1583.     st[sp]->str_pok = 1;
  1584.     if (getsockopt(fd, lvl, optname, st[sp]->str_ptr, &st[sp]->str_cur) < 0)
  1585.         goto nuts;
  1586.     break;
  1587.     case O_SSOCKOPT:
  1588.     st[sp] = st[sp+3];
  1589.     if (setsockopt(fd, lvl, optname, st[sp]->str_ptr, st[sp]->str_cur) < 0)
  1590.         goto nuts;
  1591.     st[sp] = &str_yes;
  1592.     break;
  1593.     }
  1594.     
  1595.     return sp;
  1596.  
  1597. nuts:
  1598.     if (dowarn)
  1599.     warn("[gs]etsockopt() on closed fd");
  1600.     st[sp] = &str_undef;
  1601.     errno = EBADF;
  1602.     return sp;
  1603.  
  1604. }
  1605.  
  1606. int
  1607. do_getsockname(optype, stab, arglast)
  1608. int optype;
  1609. STAB *stab;
  1610. int *arglast;
  1611. {
  1612.     register STR **st = stack->ary_array;
  1613.     register int sp = arglast[1];
  1614.     register STIO *stio;
  1615.     int fd;
  1616.  
  1617.     if (!stab)
  1618.     goto nuts;
  1619.  
  1620.     stio = stab_io(stab);
  1621.     if (!stio || !stio->ifp)
  1622.     goto nuts;
  1623.  
  1624.     st[sp] = str_2mortal(Str_new(22,257));
  1625.     st[sp]->str_cur = 256;
  1626.     st[sp]->str_pok = 1;
  1627.     fd = fileno(stio->ifp);
  1628.     switch (optype) {
  1629.     case O_GETSOCKNAME:
  1630.     if (getsockname(fd, st[sp]->str_ptr, &st[sp]->str_cur) < 0)
  1631.         goto nuts2;
  1632.     break;
  1633.     case O_GETPEERNAME:
  1634.     if (getpeername(fd, st[sp]->str_ptr, &st[sp]->str_cur) < 0)
  1635.         goto nuts2;
  1636.     break;
  1637.     }
  1638.     
  1639.     return sp;
  1640.  
  1641. nuts:
  1642.     if (dowarn)
  1643.     warn("get{sock,peer}name() on closed fd");
  1644.     errno = EBADF;
  1645. nuts2:
  1646.     st[sp] = &str_undef;
  1647.     return sp;
  1648.  
  1649. }
  1650.  
  1651. int
  1652. do_ghent(which,gimme,arglast)
  1653. int which;
  1654. int gimme;
  1655. int *arglast;
  1656. {
  1657.     register ARRAY *ary = stack;
  1658.     register int sp = arglast[0];
  1659.     register char **elem;
  1660.     register STR *str;
  1661.     struct hostent *gethostbyname();
  1662.     struct hostent *gethostbyaddr();
  1663. #ifdef HAS_GETHOSTENT
  1664.     struct hostent *gethostent();
  1665. #endif
  1666.     struct hostent *hent;
  1667.     unsigned long len;
  1668.  
  1669.     if (gimme != G_ARRAY) {
  1670.     astore(ary, ++sp, str_mortal(&str_undef));
  1671.     return sp;
  1672.     }
  1673.  
  1674.     if (which == O_GHBYNAME) {
  1675.     char *name = str_get(ary->ary_array[sp+1]);
  1676.  
  1677.     hent = gethostbyname(name);
  1678.     }
  1679.     else if (which == O_GHBYADDR) {
  1680.     STR *addrstr = ary->ary_array[sp+1];
  1681.     int addrtype = (int)str_gnum(ary->ary_array[sp+2]);
  1682.     char *addr = str_get(addrstr);
  1683.  
  1684.     hent = gethostbyaddr(addr,addrstr->str_cur,addrtype);
  1685.     }
  1686.     else
  1687. #ifdef HAS_GETHOSTENT
  1688.     hent = gethostent();
  1689. #else
  1690.     fatal("gethostent not implemented");
  1691. #endif
  1692.     if (hent) {
  1693. #ifndef lint
  1694.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1695.     str_set(str, hent->h_name);
  1696.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1697.     for (elem = hent->h_aliases; *elem; elem++) {
  1698.         str_cat(str, *elem);
  1699.         if (elem[1])
  1700.         str_ncat(str," ",1);
  1701.     }
  1702.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1703.     str_numset(str, (double)hent->h_addrtype);
  1704.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1705.     len = hent->h_length;
  1706.     str_numset(str, (double)len);
  1707. #ifdef h_addr
  1708.     for (elem = hent->h_addr_list; *elem; elem++) {
  1709.         (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1710.         str_nset(str, *elem, len);
  1711.     }
  1712. #else
  1713.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1714.     str_nset(str, hent->h_addr, len);
  1715. #endif /* h_addr */
  1716. #else /* lint */
  1717.     elem = Nullch;
  1718.     elem = elem;
  1719.     (void)astore(ary, ++sp, str_mortal(&str_no));
  1720. #endif /* lint */
  1721.     }
  1722.  
  1723.     return sp;
  1724. }
  1725.  
  1726. int
  1727. do_gnent(which,gimme,arglast)
  1728. int which;
  1729. int gimme;
  1730. int *arglast;
  1731. {
  1732.     register ARRAY *ary = stack;
  1733.     register int sp = arglast[0];
  1734.     register char **elem;
  1735.     register STR *str;
  1736.     struct netent *getnetbyname();
  1737.     struct netent *getnetbyaddr();
  1738.     struct netent *getnetent();
  1739.     struct netent *nent;
  1740.  
  1741.     if (gimme != G_ARRAY) {
  1742.     astore(ary, ++sp, str_mortal(&str_undef));
  1743.     return sp;
  1744.     }
  1745.  
  1746.     if (which == O_GNBYNAME) {
  1747.     char *name = str_get(ary->ary_array[sp+1]);
  1748.  
  1749.     nent = getnetbyname(name);
  1750.     }
  1751.     else if (which == O_GNBYADDR) {
  1752.     unsigned long addr = U_L(str_gnum(ary->ary_array[sp+1]));
  1753.     int addrtype = (int)str_gnum(ary->ary_array[sp+2]);
  1754.  
  1755.     nent = getnetbyaddr((long)addr,addrtype);
  1756.     }
  1757.     else
  1758.     nent = getnetent();
  1759.  
  1760.     if (nent) {
  1761. #ifndef lint
  1762.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1763.     str_set(str, nent->n_name);
  1764.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1765.     for (elem = nent->n_aliases; *elem; elem++) {
  1766.         str_cat(str, *elem);
  1767.         if (elem[1])
  1768.         str_ncat(str," ",1);
  1769.     }
  1770.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1771.     str_numset(str, (double)nent->n_addrtype);
  1772.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1773.     str_numset(str, (double)nent->n_net);
  1774. #else /* lint */
  1775.     elem = Nullch;
  1776.     elem = elem;
  1777.     (void)astore(ary, ++sp, str_mortal(&str_no));
  1778. #endif /* lint */
  1779.     }
  1780.  
  1781.     return sp;
  1782. }
  1783.  
  1784. int
  1785. do_gpent(which,gimme,arglast)
  1786. int which;
  1787. int gimme;
  1788. int *arglast;
  1789. {
  1790.     register ARRAY *ary = stack;
  1791.     register int sp = arglast[0];
  1792.     register char **elem;
  1793.     register STR *str;
  1794.     struct protoent *getprotobyname();
  1795.     struct protoent *getprotobynumber();
  1796.     struct protoent *getprotoent();
  1797.     struct protoent *pent;
  1798.  
  1799.     if (gimme != G_ARRAY) {
  1800.     astore(ary, ++sp, str_mortal(&str_undef));
  1801.     return sp;
  1802.     }
  1803.  
  1804.     if (which == O_GPBYNAME) {
  1805.     char *name = str_get(ary->ary_array[sp+1]);
  1806.  
  1807.     pent = getprotobyname(name);
  1808.     }
  1809.     else if (which == O_GPBYNUMBER) {
  1810.     int proto = (int)str_gnum(ary->ary_array[sp+1]);
  1811.  
  1812.     pent = getprotobynumber(proto);
  1813.     }
  1814.     else
  1815.     pent = getprotoent();
  1816.  
  1817.     if (pent) {
  1818. #ifndef lint
  1819.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1820.     str_set(str, pent->p_name);
  1821.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1822.     for (elem = pent->p_aliases; *elem; elem++) {
  1823.         str_cat(str, *elem);
  1824.         if (elem[1])
  1825.         str_ncat(str," ",1);
  1826.     }
  1827.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1828.     str_numset(str, (double)pent->p_proto);
  1829. #else /* lint */
  1830.     elem = Nullch;
  1831.     elem = elem;
  1832.     (void)astore(ary, ++sp, str_mortal(&str_no));
  1833. #endif /* lint */
  1834.     }
  1835.  
  1836.     return sp;
  1837. }
  1838.  
  1839. int
  1840. do_gsent(which,gimme,arglast)
  1841. int which;
  1842. int gimme;
  1843. int *arglast;
  1844. {
  1845.     register ARRAY *ary = stack;
  1846.     register int sp = arglast[0];
  1847.     register char **elem;
  1848.     register STR *str;
  1849.     struct servent *getservbyname();
  1850.     struct servent *getservbynumber();
  1851.     struct servent *getservent();
  1852.     struct servent *sent;
  1853.  
  1854.     if (gimme != G_ARRAY) {
  1855.     astore(ary, ++sp, str_mortal(&str_undef));
  1856.     return sp;
  1857.     }
  1858.  
  1859.     if (which == O_GSBYNAME) {
  1860.     char *name = str_get(ary->ary_array[sp+1]);
  1861.     char *proto = str_get(ary->ary_array[sp+2]);
  1862.  
  1863.     if (proto && !*proto)
  1864.         proto = Nullch;
  1865.  
  1866.     sent = getservbyname(name,proto);
  1867.     }
  1868.     else if (which == O_GSBYPORT) {
  1869.     int port = (int)str_gnum(ary->ary_array[sp+1]);
  1870.     char *proto = str_get(ary->ary_array[sp+2]);
  1871.  
  1872.     sent = getservbyport(port,proto);
  1873.     }
  1874.     else
  1875.     sent = getservent();
  1876.     if (sent) {
  1877. #ifndef lint
  1878.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1879.     str_set(str, sent->s_name);
  1880.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1881.     for (elem = sent->s_aliases; *elem; elem++) {
  1882.         str_cat(str, *elem);
  1883.         if (elem[1])
  1884.         str_ncat(str," ",1);
  1885.     }
  1886.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1887. #ifdef HAS_NTOHS
  1888.     str_numset(str, (double)ntohs(sent->s_port));
  1889. #else
  1890.     str_numset(str, (double)(sent->s_port));
  1891. #endif
  1892.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1893.     str_set(str, sent->s_proto);
  1894. #else /* lint */
  1895.     elem = Nullch;
  1896.     elem = elem;
  1897.     (void)astore(ary, ++sp, str_mortal(&str_no));
  1898. #endif /* lint */
  1899.     }
  1900.  
  1901.     return sp;
  1902. }
  1903.  
  1904. #endif /* HAS_SOCKET */
  1905.  
  1906. #ifdef HAS_SELECT
  1907. int
  1908. do_select(gimme,arglast)
  1909. int gimme;
  1910. int *arglast;
  1911. {
  1912.     register STR **st = stack->ary_array;
  1913.     register int sp = arglast[0];
  1914.     register int i;
  1915.     register int j;
  1916.     register char *s;
  1917.     register STR *str;
  1918.     double value;
  1919.     int maxlen = 0;
  1920.     int nfound;
  1921.     struct timeval timebuf;
  1922.     struct timeval *tbuf = &timebuf;
  1923.     int growsize;
  1924. #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
  1925.     int masksize;
  1926.     int offset;
  1927.     char *fd_sets[4];
  1928.     int k;
  1929.  
  1930. #if BYTEORDER & 0xf0000
  1931. #define ORDERBYTE (0x88888888 - BYTEORDER)
  1932. #else
  1933. #define ORDERBYTE (0x4444 - BYTEORDER)
  1934. #endif
  1935.  
  1936. #endif
  1937.  
  1938.     for (i = 1; i <= 3; i++) {
  1939.     j = st[sp+i]->str_cur;
  1940.     if (maxlen < j)
  1941.         maxlen = j;
  1942.     }
  1943.  
  1944. #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
  1945.     growsize = maxlen;        /* little endians can use vecs directly */
  1946. #else
  1947. #ifdef NFDBITS
  1948.  
  1949. #ifndef NBBY
  1950. #define NBBY 8
  1951. #endif
  1952.  
  1953.     masksize = NFDBITS / NBBY;
  1954. #else
  1955.     masksize = sizeof(long);    /* documented int, everyone seems to use long */
  1956. #endif
  1957.     growsize = maxlen + (masksize - (maxlen % masksize));
  1958.     Zero(&fd_sets[0], 4, char*);
  1959. #endif
  1960.  
  1961.     for (i = 1; i <= 3; i++) {
  1962.     str = st[sp+i];
  1963.     j = str->str_len;
  1964.     if (j < growsize) {
  1965.         if (str->str_pok) {
  1966.         Str_Grow(str,growsize);
  1967.         s = str_get(str) + j;
  1968.         while (++j <= growsize) {
  1969.             *s++ = '\0';
  1970.         }
  1971.         }
  1972.         else if (str->str_ptr) {
  1973.         Safefree(str->str_ptr);
  1974.         str->str_ptr = Nullch;
  1975.         }
  1976.     }
  1977. #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
  1978.     s = str->str_ptr;
  1979.     if (s) {
  1980.         New(403, fd_sets[i], growsize, char);
  1981.         for (offset = 0; offset < growsize; offset += masksize) {
  1982.         for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
  1983.             fd_sets[i][j+offset] = s[(k % masksize) + offset];
  1984.         }
  1985.     }
  1986. #endif
  1987.     }
  1988.     str = st[sp+4];
  1989.     if (str->str_nok || str->str_pok) {
  1990.     value = str_gnum(str);
  1991.     if (value < 0.0)
  1992.         value = 0.0;
  1993.     timebuf.tv_sec = (long)value;
  1994.     value -= (double)timebuf.tv_sec;
  1995.     timebuf.tv_usec = (long)(value * 1000000.0);
  1996.     }
  1997.     else
  1998.     tbuf = Null(struct timeval*);
  1999.  
  2000. #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
  2001.     nfound = select(
  2002.     maxlen * 8,
  2003.     st[sp+1]->str_ptr,
  2004.     st[sp+2]->str_ptr,
  2005.     st[sp+3]->str_ptr,
  2006.     tbuf);
  2007. #else
  2008.     nfound = select(
  2009.     maxlen * 8,
  2010.     fd_sets[1],
  2011.     fd_sets[2],
  2012.     fd_sets[3],
  2013.     tbuf);
  2014.     for (i = 1; i <= 3; i++) {
  2015.     if (fd_sets[i]) {
  2016.         str = st[sp+i];
  2017.         s = str->str_ptr;
  2018.         for (offset = 0; offset < growsize; offset += masksize) {
  2019.         for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
  2020.             s[(k % masksize) + offset] = fd_sets[i][j+offset];
  2021.         }
  2022.     }
  2023.     }
  2024. #endif
  2025.  
  2026.     st[++sp] = str_mortal(&str_no);
  2027.     str_numset(st[sp], (double)nfound);
  2028.     if (gimme == G_ARRAY && tbuf) {
  2029.     value = (double)(timebuf.tv_sec) +
  2030.         (double)(timebuf.tv_usec) / 1000000.0;
  2031.     st[++sp] = str_mortal(&str_no);
  2032.     str_numset(st[sp], value);
  2033.     }
  2034.     return sp;
  2035. }
  2036. #endif /* SELECT */
  2037.  
  2038. #ifdef HAS_SOCKET
  2039. int
  2040. do_spair(stab1, stab2, arglast)
  2041. STAB *stab1;
  2042. STAB *stab2;
  2043. int *arglast;
  2044. {
  2045.     register STR **st = stack->ary_array;
  2046.     register int sp = arglast[2];
  2047.     register STIO *stio1;
  2048.     register STIO *stio2;
  2049.     int domain, type, protocol, fd[2];
  2050.  
  2051.     if (!stab1 || !stab2)
  2052.     return FALSE;
  2053.  
  2054.     stio1 = stab_io(stab1);
  2055.     stio2 = stab_io(stab2);
  2056.     if (!stio1)
  2057.     stio1 = stab_io(stab1) = stio_new();
  2058.     else if (stio1->ifp)
  2059.     do_close(stab1,FALSE);
  2060.     if (!stio2)
  2061.     stio2 = stab_io(stab2) = stio_new();
  2062.     else if (stio2->ifp)
  2063.     do_close(stab2,FALSE);
  2064.  
  2065.     domain = (int)str_gnum(st[++sp]);
  2066.     type = (int)str_gnum(st[++sp]);
  2067.     protocol = (int)str_gnum(st[++sp]);
  2068. #ifdef TAINT
  2069.     taintproper("Insecure dependency in socketpair");
  2070. #endif
  2071. #ifdef HAS_SOCKETPAIR
  2072.     if (socketpair(domain,type,protocol,fd) < 0)
  2073.     return FALSE;
  2074. #else
  2075.     fatal("Socketpair unimplemented");
  2076. #endif
  2077.     stio1->ifp = fdopen(fd[0], "r");
  2078.     stio1->ofp = fdopen(fd[0], "w");
  2079.     stio1->type = 's';
  2080.     stio2->ifp = fdopen(fd[1], "r");
  2081.     stio2->ofp = fdopen(fd[1], "w");
  2082.     stio2->type = 's';
  2083.     if (!stio1->ifp || !stio1->ofp || !stio2->ifp || !stio2->ofp) {
  2084.     if (stio1->ifp) fclose(stio1->ifp);
  2085.     if (stio1->ofp) fclose(stio1->ofp);
  2086.     if (!stio1->ifp && !stio1->ofp) close(fd[0]);
  2087.     if (stio2->ifp) fclose(stio2->ifp);
  2088.     if (stio2->ofp) fclose(stio2->ofp);
  2089.     if (!stio2->ifp && !stio2->ofp) close(fd[1]);
  2090.     return FALSE;
  2091.     }
  2092.  
  2093.     return TRUE;
  2094. }
  2095.  
  2096. #endif /* HAS_SOCKET */
  2097.  
  2098. int
  2099. do_gpwent(which,gimme,arglast)
  2100. int which;
  2101. int gimme;
  2102. int *arglast;
  2103. {
  2104. #ifdef I_PWD
  2105.     register ARRAY *ary = stack;
  2106.     register int sp = arglast[0];
  2107.     register STR *str;
  2108.     struct passwd *getpwnam();
  2109.     struct passwd *getpwuid();
  2110.     struct passwd *getpwent();
  2111.     struct passwd *pwent;
  2112.  
  2113.     if (gimme != G_ARRAY) {
  2114.     astore(ary, ++sp, str_mortal(&str_undef));
  2115.     return sp;
  2116.     }
  2117.  
  2118.     if (which == O_GPWNAM) {
  2119.     char *name = str_get(ary->ary_array[sp+1]);
  2120.  
  2121.     pwent = getpwnam(name);
  2122.     }
  2123.     else if (which == O_GPWUID) {
  2124.     int uid = (int)str_gnum(ary->ary_array[sp+1]);
  2125.  
  2126.     pwent = getpwuid(uid);
  2127.     }
  2128.     else
  2129.     pwent = getpwent();
  2130.  
  2131.     if (pwent) {
  2132.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2133.     str_set(str, pwent->pw_name);
  2134.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2135.     str_set(str, pwent->pw_passwd);
  2136.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2137.     str_numset(str, (double)pwent->pw_uid);
  2138.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2139.     str_numset(str, (double)pwent->pw_gid);
  2140.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2141. #ifdef PWCHANGE
  2142.     str_numset(str, (double)pwent->pw_change);
  2143. #else
  2144. #ifdef PWQUOTA
  2145.     str_numset(str, (double)pwent->pw_quota);
  2146. #else
  2147. #ifdef PWAGE
  2148.     str_set(str, pwent->pw_age);
  2149. #endif
  2150. #endif
  2151. #endif
  2152.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2153. #ifdef PWCLASS
  2154.     str_set(str,pwent->pw_class);
  2155. #else
  2156. #ifdef PWCOMMENT
  2157.     str_set(str, pwent->pw_comment);
  2158. #endif
  2159. #endif
  2160.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2161.     str_set(str, pwent->pw_gecos);
  2162.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2163.     str_set(str, pwent->pw_dir);
  2164.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2165.     str_set(str, pwent->pw_shell);
  2166. #ifdef PWEXPIRE
  2167.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2168.     str_numset(str, (double)pwent->pw_expire);
  2169. #endif
  2170.     }
  2171.  
  2172.     return sp;
  2173. #else
  2174.     fatal("password routines not implemented");
  2175. #endif
  2176. }
  2177.  
  2178. int
  2179. do_ggrent(which,gimme,arglast)
  2180. int which;
  2181. int gimme;
  2182. int *arglast;
  2183. {
  2184. #ifdef I_GRP
  2185.     register ARRAY *ary = stack;
  2186.     register int sp = arglast[0];
  2187.     register char **elem;
  2188.     register STR *str;
  2189.     struct group *getgrnam();
  2190.     struct group *getgrgid();
  2191.     struct group *getgrent();
  2192.     struct group *grent;
  2193.  
  2194.     if (gimme != G_ARRAY) {
  2195.     astore(ary, ++sp, str_mortal(&str_undef));
  2196.     return sp;
  2197.     }
  2198.  
  2199.     if (which == O_GGRNAM) {
  2200.     char *name = str_get(ary->ary_array[sp+1]);
  2201.  
  2202.     grent = getgrnam(name);
  2203.     }
  2204.     else if (which == O_GGRGID) {
  2205.     int gid = (int)str_gnum(ary->ary_array[sp+1]);
  2206.  
  2207.     grent = getgrgid(gid);
  2208.     }
  2209.     else
  2210.     grent = getgrent();
  2211.  
  2212.     if (grent) {
  2213.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2214.     str_set(str, grent->gr_name);
  2215.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2216.     str_set(str, grent->gr_passwd);
  2217.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2218.     str_numset(str, (double)grent->gr_gid);
  2219.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2220.     for (elem = grent->gr_mem; *elem; elem++) {
  2221.         str_cat(str, *elem);
  2222.         if (elem[1])
  2223.         str_ncat(str," ",1);
  2224.     }
  2225.     }
  2226.  
  2227.     return sp;
  2228. #else
  2229.     fatal("group routines not implemented");
  2230. #endif
  2231. }
  2232.  
  2233. int
  2234. do_dirop(optype,stab,gimme,arglast)
  2235. int optype;
  2236. STAB *stab;
  2237. int gimme;
  2238. int *arglast;
  2239. {
  2240. #if defined(DIRENT) && defined(HAS_READDIR)
  2241.     register ARRAY *ary = stack;
  2242.     register STR **st = ary->ary_array;
  2243.     register int sp = arglast[1];
  2244.     register STIO *stio;
  2245.     long along;
  2246. #ifndef telldir
  2247.     long telldir();
  2248. #endif
  2249. #ifndef apollo
  2250.     struct DIRENT *readdir();
  2251. #endif
  2252.     register struct DIRENT *dp;
  2253.  
  2254.     if (!stab)
  2255.     goto nope;
  2256.     if (!(stio = stab_io(stab)))
  2257.     stio = stab_io(stab) = stio_new();
  2258.     if (!stio->dirp && optype != O_OPEN_DIR)
  2259.     goto nope;
  2260.     st[sp] = &str_yes;
  2261.     switch (optype) {
  2262.     case O_OPEN_DIR:
  2263.     if (stio->dirp)
  2264.         closedir(stio->dirp);
  2265.     if (!(stio->dirp = opendir(str_get(st[sp+1]))))
  2266.         goto nope;
  2267.     break;
  2268.     case O_READDIR:
  2269.     if (gimme == G_ARRAY) {
  2270.         --sp;
  2271.         /*SUPPRESS 560*/
  2272.         while (dp = readdir(stio->dirp)) {
  2273. #ifdef DIRNAMLEN
  2274.         (void)astore(ary,++sp,
  2275.           str_2mortal(str_make(dp->d_name,dp->d_namlen)));
  2276. #else
  2277.         (void)astore(ary,++sp,
  2278.           str_2mortal(str_make(dp->d_name,0)));
  2279. #endif
  2280.         }
  2281.     }
  2282.     else {
  2283.         if (!(dp = readdir(stio->dirp)))
  2284.         goto nope;
  2285.         st[sp] = str_mortal(&str_undef);
  2286. #ifdef DIRNAMLEN
  2287.         str_nset(st[sp], dp->d_name, dp->d_namlen);
  2288. #else
  2289.         str_set(st[sp], dp->d_name);
  2290. #endif
  2291.     }
  2292.     break;
  2293. #if MACH
  2294.     case O_TELLDIR:
  2295.     case O_SEEKDIR:
  2296.         goto nope;
  2297. #else
  2298.     case O_TELLDIR:
  2299.     st[sp] = str_mortal(&str_undef);
  2300.     str_numset(st[sp], (double)telldir(stio->dirp));
  2301.     break;
  2302.     case O_SEEKDIR:
  2303.     st[sp] = str_mortal(&str_undef);
  2304.     along = (long)str_gnum(st[sp+1]);
  2305.     (void)seekdir(stio->dirp,along);
  2306.     break;
  2307. #endif
  2308.     case O_REWINDDIR:
  2309.     st[sp] = str_mortal(&str_undef);
  2310.     (void)rewinddir(stio->dirp);
  2311.     break;
  2312.     case O_CLOSEDIR:
  2313.     st[sp] = str_mortal(&str_undef);
  2314.     (void)closedir(stio->dirp);
  2315.     stio->dirp = 0;
  2316.     break;
  2317.     }
  2318.     return sp;
  2319.  
  2320. nope:
  2321.     st[sp] = &str_undef;
  2322.     if (!errno)
  2323.     errno = EBADF;
  2324.     return sp;
  2325.  
  2326. #else
  2327.     fatal("Unimplemented directory operation");
  2328. #endif
  2329. }
  2330.  
  2331. apply(type,arglast)
  2332. int type;
  2333. int *arglast;
  2334. {
  2335.     register STR **st = stack->ary_array;
  2336.     register int sp = arglast[1];
  2337.     register int items = arglast[2] - sp;
  2338.     register int val;
  2339.     register int val2;
  2340.     register int tot = 0;
  2341.     char *s;
  2342.  
  2343. #ifdef TAINT
  2344.     for (st += ++sp; items--; st++)
  2345.     tainted |= (*st)->str_tainted;
  2346.     st = stack->ary_array;
  2347.     sp = arglast[1];
  2348.     items = arglast[2] - sp;
  2349. #endif
  2350.     switch (type) {
  2351.     case O_CHMOD:
  2352. #ifdef TAINT
  2353.     taintproper("Insecure dependency in chmod");
  2354. #endif
  2355.     if (--items > 0) {
  2356.         tot = items;
  2357.         val = (int)str_gnum(st[++sp]);
  2358.         while (items--) {
  2359.         if (chmod(str_get(st[++sp]),val))
  2360.             tot--;
  2361.         }
  2362.     }
  2363.     break;
  2364. #ifdef HAS_CHOWN
  2365.     case O_CHOWN:
  2366. #ifdef TAINT
  2367.     taintproper("Insecure dependency in chown");
  2368. #endif
  2369.     if (items > 2) {
  2370.         items -= 2;
  2371.         tot = items;
  2372.         val = (int)str_gnum(st[++sp]);
  2373.         val2 = (int)str_gnum(st[++sp]);
  2374.         while (items--) {
  2375.         if (chown(str_get(st[++sp]),val,val2))
  2376.             tot--;
  2377.         }
  2378.     }
  2379.     break;
  2380. #endif
  2381. #ifdef HAS_KILL
  2382.     case O_KILL:
  2383. #ifdef TAINT
  2384.     taintproper("Insecure dependency in kill");
  2385. #endif
  2386.     if (--items > 0) {
  2387.         tot = items;
  2388.         s = str_get(st[++sp]);
  2389.         if (isUPPER(*s)) {
  2390.         if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
  2391.             s += 3;
  2392.         if (!(val = whichsig(s)))
  2393.             fatal("Unrecognized signal name \"%s\"",s);
  2394.         }
  2395.         else
  2396.         val = (int)str_gnum(st[sp]);
  2397.         if (val < 0) {
  2398.         val = -val;
  2399.         while (items--) {
  2400.             int proc = (int)str_gnum(st[++sp]);
  2401. #ifdef HAS_KILLPG
  2402.             if (killpg(proc,val))    /* BSD */
  2403. #else
  2404.             if (kill(-proc,val))    /* SYSV */
  2405. #endif
  2406.             tot--;
  2407.         }
  2408.         }
  2409.         else {
  2410.         while (items--) {
  2411.             if (kill((int)(str_gnum(st[++sp])),val))
  2412.             tot--;
  2413.         }
  2414.         }
  2415.     }
  2416.     break;
  2417. #endif
  2418.     case O_UNLINK:
  2419. #ifdef TAINT
  2420.     taintproper("Insecure dependency in unlink");
  2421. #endif
  2422.     tot = items;
  2423.     while (items--) {
  2424.         s = str_get(st[++sp]);
  2425.         if (euid || unsafe) {
  2426.             if (UNLINK(s))
  2427.                 tot--;
  2428.         }
  2429.         else {    /* don't let root wipe out directories without -U */
  2430. #ifdef HAS_LSTAT
  2431.         if (lstat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
  2432. #else
  2433.         if (stat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
  2434. #endif
  2435.             tot--;
  2436.         else {
  2437.             if (UNLINK(s))
  2438.             tot--;
  2439.         }
  2440.         }
  2441.     }
  2442.     break;
  2443.     case O_UTIME:
  2444. #ifdef TAINT
  2445.     taintproper("Insecure dependency in utime");
  2446. #endif
  2447.     if (items > 2) {
  2448. #ifdef I_UTIME
  2449.         struct utimbuf utbuf;
  2450. #else
  2451.         struct {
  2452.         long    actime;
  2453.         long    modtime;
  2454.         } utbuf;
  2455. #endif
  2456.  
  2457.         Zero(&utbuf, sizeof utbuf, char);
  2458.         utbuf.actime = (long)str_gnum(st[++sp]);    /* time accessed */
  2459.         utbuf.modtime = (long)str_gnum(st[++sp]);    /* time modified */
  2460.         items -= 2;
  2461. #ifndef lint
  2462.         tot = items;
  2463.         while (items--) {
  2464.         if (utime(str_get(st[++sp]),&utbuf))
  2465.             tot--;
  2466.         }
  2467. #endif
  2468.     }
  2469.     else
  2470.         items = 0;
  2471.     break;
  2472.     }
  2473.     return tot;
  2474. }
  2475.  
  2476. /* Do the permissions allow some operation?  Assumes statcache already set. */
  2477.  
  2478. int
  2479. cando(bit, effective, statbufp)
  2480. int bit;
  2481. int effective;
  2482. register struct stat *statbufp;
  2483. {
  2484. #ifdef MSDOS
  2485.     /* [Comments and code from Len Reed]
  2486.      * MS-DOS "user" is similar to UNIX's "superuser," but can't write
  2487.      * to write-protected files.  The execute permission bit is set
  2488.      * by the Miscrosoft C library stat() function for the following:
  2489.      *        .exe files
  2490.      *        .com files
  2491.      *        .bat files
  2492.      *        directories
  2493.      * All files and directories are readable.
  2494.      * Directories and special files, e.g. "CON", cannot be
  2495.      * write-protected.
  2496.      * [Comment by Tom Dinger -- a directory can have the write-protect
  2497.      *        bit set in the file system, but DOS permits changes to
  2498.      *        the directory anyway.  In addition, all bets are off
  2499.      *        here for networked software, such as Novell and
  2500.      *        Sun's PC-NFS.]
  2501.      */
  2502.  
  2503.      return (bit & statbufp->st_mode) ? TRUE : FALSE;
  2504.  
  2505. #else /* ! MSDOS */
  2506.     if ((effective ? euid : uid) == 0) {    /* root is special */
  2507.     if (bit == S_IXUSR) {
  2508.         if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
  2509.         return TRUE;
  2510.     }
  2511.     else
  2512.         return TRUE;        /* root reads and writes anything */
  2513.     return FALSE;
  2514.     }
  2515.     if (statbufp->st_uid == (effective ? euid : uid) ) {
  2516.     if (statbufp->st_mode & bit)
  2517.         return TRUE;    /* ok as "user" */
  2518.     }
  2519.     else if (ingroup((int)statbufp->st_gid,effective)) {
  2520.     if (statbufp->st_mode & bit >> 3)
  2521.         return TRUE;    /* ok as "group" */
  2522.     }
  2523.     else if (statbufp->st_mode & bit >> 6)
  2524.     return TRUE;    /* ok as "other" */
  2525.     return FALSE;
  2526. #endif /* ! MSDOS */
  2527. }
  2528.  
  2529. int
  2530. ingroup(testgid,effective)
  2531. int testgid;
  2532. int effective;
  2533. {
  2534. #ifdef macintosh
  2535.     /* This is simply not correct for AppleShare, but fix it yerself. */
  2536.     return TRUE;
  2537. #else
  2538.     if (testgid == (effective ? egid : gid))
  2539.     return TRUE;
  2540. #ifdef HAS_GETGROUPS
  2541. #ifndef NGROUPS
  2542. #define NGROUPS 32
  2543. #endif
  2544.     {
  2545.     GROUPSTYPE gary[NGROUPS];
  2546.     int anum;
  2547.  
  2548.     anum = getgroups(NGROUPS,gary);
  2549.     while (--anum >= 0)
  2550.         if (gary[anum] == testgid)
  2551.         return TRUE;
  2552.     }
  2553. #endif
  2554.     return FALSE;
  2555. #endif
  2556. }
  2557.  
  2558. #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
  2559.  
  2560. int
  2561. do_ipcget(optype, arglast)
  2562. int optype;
  2563. int *arglast;
  2564. {
  2565.     register STR **st = stack->ary_array;
  2566.     register int sp = arglast[0];
  2567.     key_t key;
  2568.     int n, flags;
  2569.  
  2570.     key = (key_t)str_gnum(st[++sp]);
  2571.     n = (optype == O_MSGGET) ? 0 : (int)str_gnum(st[++sp]);
  2572.     flags = (int)str_gnum(st[++sp]);
  2573.     errno = 0;
  2574.     switch (optype)
  2575.     {
  2576. #ifdef HAS_MSG
  2577.     case O_MSGGET:
  2578.     return msgget(key, flags);
  2579. #endif
  2580. #ifdef HAS_SEM
  2581.     case O_SEMGET:
  2582.     return semget(key, n, flags);
  2583. #endif
  2584. #ifdef HAS_SHM
  2585.     case O_SHMGET:
  2586.     return shmget(key, n, flags);
  2587. #endif
  2588. #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
  2589.     default:
  2590.     fatal("%s not implemented", opname[optype]);
  2591. #endif
  2592.     }
  2593.     return -1;            /* should never happen */
  2594. }
  2595.  
  2596. int
  2597. do_ipcctl(optype, arglast)
  2598. int optype;
  2599. int *arglast;
  2600. {
  2601.     register STR **st = stack->ary_array;
  2602.     register int sp = arglast[0];
  2603.     STR *astr;
  2604.     char *a;
  2605.     int id, n, cmd, infosize, getinfo, ret;
  2606.  
  2607.     id = (int)str_gnum(st[++sp]);
  2608.     n = (optype == O_SEMCTL) ? (int)str_gnum(st[++sp]) : 0;
  2609.     cmd = (int)str_gnum(st[++sp]);
  2610.     astr = st[++sp];
  2611.  
  2612.     infosize = 0;
  2613.     getinfo = (cmd == IPC_STAT);
  2614.  
  2615.     switch (optype)
  2616.     {
  2617. #ifdef HAS_MSG
  2618.     case O_MSGCTL:
  2619.     if (cmd == IPC_STAT || cmd == IPC_SET)
  2620.         infosize = sizeof(struct msqid_ds);
  2621.     break;
  2622. #endif
  2623. #ifdef HAS_SHM
  2624.     case O_SHMCTL:
  2625.     if (cmd == IPC_STAT || cmd == IPC_SET)
  2626.         infosize = sizeof(struct shmid_ds);
  2627.     break;
  2628. #endif
  2629. #ifdef HAS_SEM
  2630.     case O_SEMCTL:
  2631.     if (cmd == IPC_STAT || cmd == IPC_SET)
  2632.         infosize = sizeof(struct semid_ds);
  2633.     else if (cmd == GETALL || cmd == SETALL)
  2634.     {
  2635.         struct semid_ds semds;
  2636.         if (semctl(id, 0, IPC_STAT, &semds) == -1)
  2637.         return -1;
  2638.         getinfo = (cmd == GETALL);
  2639.         infosize = semds.sem_nsems * sizeof(short);
  2640.         /* "short" is technically wrong but much more portable
  2641.            than guessing about u_?short(_t)? */
  2642.     }
  2643.     break;
  2644. #endif
  2645. #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
  2646.     default:
  2647.     fatal("%s not implemented", opname[optype]);
  2648. #endif
  2649.     }
  2650.  
  2651.     if (infosize)
  2652.     {
  2653.     if (getinfo)
  2654.     {
  2655.         STR_GROW(astr, infosize+1);
  2656.         a = str_get(astr);
  2657.     }
  2658.     else
  2659.     {
  2660.         a = str_get(astr);
  2661.         if (astr->str_cur != infosize)
  2662.         {
  2663.         errno = EINVAL;
  2664.         return -1;
  2665.         }
  2666.     }
  2667.     }
  2668.     else
  2669.     {
  2670.     int i = (int)str_gnum(astr);
  2671.     a = (char *)i;        /* ouch */
  2672.     }
  2673.     errno = 0;
  2674.     switch (optype)
  2675.     {
  2676. #ifdef HAS_MSG
  2677.     case O_MSGCTL:
  2678.     ret = msgctl(id, cmd, a);
  2679.     break;
  2680. #endif
  2681. #ifdef HAS_SEM
  2682.     case O_SEMCTL:
  2683.     ret = semctl(id, n, cmd, a);
  2684.     break;
  2685. #endif
  2686. #ifdef HAS_SHM
  2687.     case O_SHMCTL:
  2688.     ret = shmctl(id, cmd, a);
  2689.     break;
  2690. #endif
  2691.     }
  2692.     if (getinfo && ret >= 0) {
  2693.     astr->str_cur = infosize;
  2694.     astr->str_ptr[infosize] = '\0';
  2695.     }
  2696.     return ret;
  2697. }
  2698.  
  2699. int
  2700. do_msgsnd(arglast)
  2701. int *arglast;
  2702. {
  2703. #ifdef HAS_MSG
  2704.     register STR **st = stack->ary_array;
  2705.     register int sp = arglast[0];
  2706.     STR *mstr;
  2707.     char *mbuf;
  2708.     int id, msize, flags;
  2709.  
  2710.     id = (int)str_gnum(st[++sp]);
  2711.     mstr = st[++sp];
  2712.     flags = (int)str_gnum(st[++sp]);
  2713.     mbuf = str_get(mstr);
  2714.     if ((msize = mstr->str_cur - sizeof(long)) < 0) {
  2715.     errno = EINVAL;
  2716.     return -1;
  2717.     }
  2718.     errno = 0;
  2719.     return msgsnd(id, mbuf, msize, flags);
  2720. #else
  2721.     fatal("msgsnd not implemented");
  2722. #endif
  2723. }
  2724.  
  2725. int
  2726. do_msgrcv(arglast)
  2727. int *arglast;
  2728. {
  2729. #ifdef HAS_MSG
  2730.     register STR **st = stack->ary_array;
  2731.     register int sp = arglast[0];
  2732.     STR *mstr;
  2733.     char *mbuf;
  2734.     long mtype;
  2735.     int id, msize, flags, ret;
  2736.  
  2737.     id = (int)str_gnum(st[++sp]);
  2738.     mstr = st[++sp];
  2739.     msize = (int)str_gnum(st[++sp]);
  2740.     mtype = (long)str_gnum(st[++sp]);
  2741.     flags = (int)str_gnum(st[++sp]);
  2742.     mbuf = str_get(mstr);
  2743.     if (mstr->str_cur < sizeof(long)+msize+1) {
  2744.     STR_GROW(mstr, sizeof(long)+msize+1);
  2745.     mbuf = str_get(mstr);
  2746.     }
  2747.     errno = 0;
  2748.     ret = msgrcv(id, mbuf, msize, mtype, flags);
  2749.     if (ret >= 0) {
  2750.     mstr->str_cur = sizeof(long)+ret;
  2751.     mstr->str_ptr[sizeof(long)+ret] = '\0';
  2752.     }
  2753.     return ret;
  2754. #else
  2755.     fatal("msgrcv not implemented");
  2756. #endif
  2757. }
  2758.  
  2759. int
  2760. do_semop(arglast)
  2761. int *arglast;
  2762. {
  2763. #ifdef HAS_SEM
  2764.     register STR **st = stack->ary_array;
  2765.     register int sp = arglast[0];
  2766.     STR *opstr;
  2767.     char *opbuf;
  2768.     int id, opsize;
  2769.  
  2770.     id = (int)str_gnum(st[++sp]);
  2771.     opstr = st[++sp];
  2772.     opbuf = str_get(opstr);
  2773.     opsize = opstr->str_cur;
  2774.     if (opsize < sizeof(struct sembuf)
  2775.     || (opsize % sizeof(struct sembuf)) != 0) {
  2776.     errno = EINVAL;
  2777.     return -1;
  2778.     }
  2779.     errno = 0;
  2780.     return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
  2781. #else
  2782.     fatal("semop not implemented");
  2783. #endif
  2784. }
  2785.  
  2786. int
  2787. do_shmio(optype, arglast)
  2788. int optype;
  2789. int *arglast;
  2790. {
  2791. #ifdef HAS_SHM
  2792.     register STR **st = stack->ary_array;
  2793.     register int sp = arglast[0];
  2794.     STR *mstr;
  2795.     char *mbuf, *shm;
  2796.     int id, mpos, msize;
  2797.     struct shmid_ds shmds;
  2798. #ifndef VOIDSHMAT
  2799.     extern char *shmat();
  2800. #endif
  2801.  
  2802.     id = (int)str_gnum(st[++sp]);
  2803.     mstr = st[++sp];
  2804.     mpos = (int)str_gnum(st[++sp]);
  2805.     msize = (int)str_gnum(st[++sp]);
  2806.     errno = 0;
  2807.     if (shmctl(id, IPC_STAT, &shmds) == -1)
  2808.     return -1;
  2809.     if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
  2810.     errno = EFAULT;        /* can't do as caller requested */
  2811.     return -1;
  2812.     }
  2813.     shm = (char*)shmat(id, (char*)NULL, (optype == O_SHMREAD) ? SHM_RDONLY : 0);
  2814.     if (shm == (char *)-1)    /* I hate System V IPC, I really do */
  2815.     return -1;
  2816.     mbuf = str_get(mstr);
  2817.     if (optype == O_SHMREAD) {
  2818.     if (mstr->str_cur < msize) {
  2819.         STR_GROW(mstr, msize+1);
  2820.         mbuf = str_get(mstr);
  2821.     }
  2822.     bcopy(shm + mpos, mbuf, msize);
  2823.     mstr->str_cur = msize;
  2824.     mstr->str_ptr[msize] = '\0';
  2825.     }
  2826.     else {
  2827.     int n;
  2828.  
  2829.     if ((n = mstr->str_cur) > msize)
  2830.         n = msize;
  2831.     bcopy(mbuf, shm + mpos, n);
  2832.     if (n < msize)
  2833.         bzero(shm + mpos + n, msize - n);
  2834.     }
  2835.     return shmdt(shm);
  2836. #else
  2837.     fatal("shm I/O not implemented");
  2838. #endif
  2839. }
  2840.  
  2841. #endif /* SYSV IPC */
  2842.